package dao.concret;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import dao.AbstractTable;
import dao.DAOList;
import dao.tables.SPECIALITE;

public class SpecialiteDAO extends DAOList<SPECIALITE> {

	@Override
	public SPECIALITE find(long id) {
		SPECIALITE spec = new SPECIALITE();
		try {
			Statement stmt = this.connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
				ResultSet.CONCUR_UPDATABLE);
			ResultSet result = stmt.executeQuery("SELECT * FROM SPECIALITE WHERE IDSPECIALITE = "
				+ id);
			if (result.first()) spec = new SPECIALITE(id, result.getString("SPECIALITE"));
			stmt.close();
			result.close();
			stmt = null;
			result = null;
		} catch (SQLException sqle) {
			sqle.printStackTrace();
		}
		return spec;
	}

	@Override
	public SPECIALITE create(AbstractTable obj) {
		return null;
	}

	@Override
	public SPECIALITE update(AbstractTable obj) {
		try {
			Statement stmt = this.connect.createStatement();
			stmt.executeUpdate("UPDATE SPECIALITE SET SPECIALITE = '" + obj.getValue()
				+ "' WHERE IDSPECIALITE = " + obj.getId());
			obj = this.find(obj.getId());
			stmt.close();
		} catch (SQLException sqle) {
			sqle.printStackTrace();
		}
		return (SPECIALITE) obj;
	}

	@Override
	public SPECIALITE delete(AbstractTable obj) {
		return null;
	}

	@Override
	public ArrayList<SPECIALITE> list() {
		ArrayList<SPECIALITE> list = new ArrayList<SPECIALITE>();
		try {
			Statement stmt = this.connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
				ResultSet.CONCUR_UPDATABLE);
			ResultSet result = stmt.executeQuery("SELECT IDSPECIALITE FROM SPECIALITE");
			while (result.next())
				list.add(this.find(result.getLong("IDSPECIALITE")));
			stmt.close();
			result.close();
			stmt = null;
			result = null;
		} catch (SQLException sqle) {
			sqle.printStackTrace();
		}
		return list;
	}

	@Override
	public ArrayList<SPECIALITE> list(long id) {
		ArrayList<SPECIALITE> list = new ArrayList<SPECIALITE>();
		try {
			Statement stmt = this.connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
				ResultSet.CONCUR_UPDATABLE);
			ResultSet result = stmt
				.executeQuery("SELECT IDSPECIALITE FROM SPECIALISER WHERE IDPERSONNE = " + id);
			while (result.next())
				list.add(this.find(result.getLong("IDSPECIALITE")));
			result.close();
			stmt.close();
			stmt = null;
			result = null;
		} catch (SQLException sqle) {
			sqle.printStackTrace();
		}
		return list;
	}
}
